package com.aebiz.usercenter.goods.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.aebiz.usercenter.common.util.PageInfoUtil;
import com.aebiz.usercenter.goods.dao.IAGoodsBrandDao;
import com.aebiz.usercenter.goods.dao.IAGoodsCateDao;
import com.aebiz.usercenter.goods.pojo.AGoods;
import com.aebiz.usercenter.goods.pojo.AGoodsBrand;
import com.aebiz.usercenter.goods.pojo.AGoodsCate;
import com.aebiz.usercenter.goods.service.IGoodsService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService{

	@Resource
	private IAGoodsBrandDao goodsBrandDao;
	
	@Resource
	private IAGoodsCateDao goodsCateDao;
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand, Map<String, Object> paramsMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand, Map<String, Object> paramsMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject deleteOneGoodsBrandService(AGoodsBrand goodsBrand) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AGoodsBrand findOneGoodsBrandService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> findCondListGoodsBrandService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AGoodsBrand> roleList = this.goodsBrandDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AGoodsBrand> roleList= this.goodsBrandDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}

	@Override
	public JSONObject saveOneGoodsCateService(AGoodsCate goodsCate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject updateOneGoodsCateService(AGoodsCate goodsCate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject deleteOneGoodsCateService(AGoodsCate goodsCate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AGoodsCate findOneGoodsCateService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> findCondListGoodsCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		Map<String, Object> resultMap=new HashMap<>();
		if(condMap.get("keyword")!=null&&!"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null)
		{
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoodsCate> goodCateList=this.goodsCateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", goodCateList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		List<AGoodsCate> goodCateList=this.goodsCateDao.findList(condMap);
		resultMap.put("list", goodCateList);
		return resultMap;
	}

	@Override
	public List<AGoodsCate> findTreeListCateService() {
		/*储存结果*/
		List<AGoodsCate> resultList=new ArrayList<AGoodsCate>();
		/*先查询出所有的一级节点*/
		Map<String,Object> condMap=new HashMap<>();
		condMap.put("parentId", "0");
		Map<String, Object> resultMap=this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> cateList=(List<AGoodsCate>) resultMap.get("list");
		for (Iterator iterator = cateList.iterator(); iterator.hasNext();) {
			AGoodsCate cateTemp = (AGoodsCate) iterator.next();
			this.findTreeListCateServiceUtil(cateTemp,0,resultList);
		}
		return resultList;
	}

	/**
	 * 列出树形结构图的递归
	 * 存储结果：用参数存储结果呢
	 * @param cateTemp
	 * @param i
	 * @param resultList
	 */
	private void findTreeListCateServiceUtil(AGoodsCate cate, int level, List<AGoodsCate> resultList) {

		StringBuffer preStrsb=new StringBuffer();
		for (int i = 0; i < level; i++) {
			preStrsb.append("|-->");
		}
		/* 列出树形结构 */
		System.out.println(preStrsb.toString() + cate.getName());
		cate.setTreeName(preStrsb.toString()+cate.getName());
		/*增加到容器中去*/
		resultList.add(cate);
		/* 查询子节点 
		 * 查询出的子节点才有可能不一样
		 * */
		Map<String, Object> condMap=new HashMap<>();
		condMap.put("parentId", cate.getId());
		Map<String, Object> childMap=this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> childList=(List<AGoodsCate>) childMap.get("list");
		if(childList.size()>0)
		{
			cate.setLeafStatus("0");
			for (Iterator iterator = childList.iterator(); iterator.hasNext();) {
				AGoodsCate cateTmpe = (AGoodsCate) iterator.next();
				/* 自己调用自己,参数会发生变化, */
				this.findTreeListCateServiceUtil(cateTmpe,level + 1,resultList);
			}
		}else {
			cate.setLeafStatus("1");
		}
	}

	@Override
	public JSONObject saveOneGoodsService(AGoods goods) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject saveOneGoodsService(AGoods goods, Map<String, Object> paramsMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject updateOneGoodsService(AGoods goods) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject deleteOneGoodsService(AGoods goods) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AGoods findOneGoodsService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> findCondListGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> findCondListUsersCarService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

}
